package com.lrkj.payfee.service.impl;

import java.util.List;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.payfee.domain.CopyRoom;
import com.lrkj.property.domain.Community;
import com.lrkj.property.domain.Room;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.property.service.IRoomService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.payfee.mapper.GenerateMapper;
import com.lrkj.payfee.domain.Generate;
import com.lrkj.payfee.service.IGenerateService;

/**
 * 生成仪费用Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-30
 */
@Service
public class GenerateServiceImpl implements IGenerateService
{
    @Autowired
    private GenerateMapper generateMapper;

    @Autowired
    private ICommunityService communityService;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private IRoomService roomService;

    /**
     * 查询生成仪费用
     *
     * @param generateId 生成仪费用ID
     * @return 生成仪费用
     */
    @Override
    public Generate selectGenerateById(String generateId)
    {
        return generateMapper.selectGenerateById(generateId);
    }

    /**
     * 查询生成仪费用列表
     *
     * @param generate 生成仪费用
     * @return 生成仪费用
     */
    @Override
    public List<Generate> selectGenerateList(Generate generate)
    {
        return generateMapper.selectGenerateList(generate);
    }

    /**
     * 新增生成仪费用
     *
     * @param generate 生成仪费用
     * @return 结果
     */
    @Override
    public int insertGenerate(Generate generate)
    {
        generate.setGenerateId(IdUtils.simpleUUID());
        generate.setCreateBy(SecurityUtils.getUsername());
        generate.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(generate);
        return generateMapper.insertGenerate(generate);
    }

    /**
     * 修改生成仪费用
     *
     * @param generate 生成仪费用
     * @return 结果
     */
    @Override
    public int updateGenerate(Generate generate)
    {
        this.setUnitInfo(generate);
        generate.setUpdateBy(SecurityUtils.getUsername());
        generate.setUpdateTime(DateUtils.getNowDate());
        return generateMapper.updateGenerate(generate);
    }

    /**
     * 批量删除生成仪费用
     *
     * @param generateIds 需要删除的生成仪费用ID
     * @return 结果
     */
    @Override
    public int deleteGenerateByIds(String[] generateIds)
    {
        orgTreeService.deleteOrgTreeByIds(generateIds);
        return generateMapper.deleteGenerateByIds(generateIds);
    }

    /**
     * 删除生成仪费用信息
     *
     * @param generateId 生成仪费用ID
     * @return 结果
     */
    @Override
    public int deleteGenerateById(String generateId)
    {
        orgTreeService.deleteOrgTreeById(generateId);
        return generateMapper.deleteGenerateById(generateId);
    }

    @Override
    public int updateGenCopyId(Generate generate) {
        generate.setUpdateBy(SecurityUtils.getUsername());
        generate.setUpdateTime(DateUtils.getNowDate());
        return this.generateMapper.updateGenCopyId(generate);
    }

    @Override
    public int deleteGenCopyId(String copyId) {

        return generateMapper.deleteGenCopyId(copyId);
    }

    @Override
    public int deleteGenCopyIds(String copyIds) {
        return generateMapper.deleteGenCopyIds(copyIds);
    }

    @Override
    public int updateState(String generateId) {
        return generateMapper.updateState(generateId);
    }

    @Override
    public Generate selectCopyId(String copyId) {
        return generateMapper.selectCopyId(copyId);
    }


    private void setUnitInfo(Generate bean) {
        Room room = roomService.selectRoomById(bean.getRoomId());
        bean.setAncestors(room.getAncestors() + "," + room.getId());
        bean.setDeptId(room.getDeptId());
        //orgTreeService.insertOrgTree(bean.getGenerateId(),room.getOwnerVo().getOwnerName(), bean.getGenerateId(), OrgConstants.gener, room.getAncestors());
    }
}
